using CommunityToolkit.Mvvm.ComponentModel;
using System.Collections.Generic;
using System.Windows.Controls;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using MinoChat.Services.Interfaces;

namespace MinoChat.Services;

public class NavigationService : INavigationService
{
    private readonly IServiceProvider _serviceProvider;
    private readonly ILogger<NavigationService> _logger;
    //导航载体控件
    private ContentControl? _contentControl;
    //导航到的页面
    public UserControl? CurrentPage { get; private set; }
    //导航历史栈
    //Stack<T>是一个泛型集合，表示后进先出LIFO的对象集合
    private readonly Stack<UserControl> _navigationStack = new();
    //定义属性，表示是否可以返回上一页
    public bool CanGoBack => _navigationStack.Count > 0;

    //host中默认就会注入IServicsProvide，这里直接依赖注入
    public NavigationService(IServiceProvider serviceProvider, ILogger<NavigationService> logger)
    {
        _serviceProvider = serviceProvider;
        _logger = logger;
    }
    public void SetNavigationControl(ContentControl contentControl)
    {
        _contentControl = contentControl;
        _logger.LogInformation("初始化导航服务控件，ContentControl: {IsNotNull}", contentControl != null);
    }

    public void NavigateTo<T>() where T : UserControl
    {
        _logger.LogDebug("准备导航到页面: {PageName}, ContentControl可用: {IsAvailable}", typeof(T).Name, _contentControl != null);

        if (_contentControl == null)
        {
            _logger.LogError("ContentControl为空，无法导航");
            return;
        }

        // 如果目标页面是当前页面，不执行导航
        if (CurrentPage?.GetType() == typeof(T))
        {
            _logger.LogDebug("目标页面与当前页面相同，跳过导航: {PageName}", typeof(T).Name);
            return;
        }

        // 如果当前页面存在，将其压入导航栈
        if (CurrentPage != null)
        {
            _navigationStack.Push(CurrentPage);
            _logger.LogDebug("将 {CurrentPageName} 压入导航栈，栈深度: {StackCount}", CurrentPage.GetType().Name, _navigationStack.Count);
        }

        CurrentPage = _serviceProvider.GetRequiredService<T>();
        _logger.LogDebug("创建了新页面: {PageCreated}", CurrentPage != null);
        _contentControl.Content = CurrentPage;
        _logger.LogInformation("导航到页面: {PageName}", typeof(T).Name);
    }

    public void NavigateTo(UserControl page)
    {
        _logger.LogDebug("直接导航到页面: {PageName}, ContentControl可用: {IsAvailable}", page?.GetType().Name, _contentControl != null);

        if (_contentControl == null)
        {
            _logger.LogError("ContentControl为空，无法导航");
            return;
        }

        // 如果目标页面是当前页面，不执行导航
        if (CurrentPage?.GetType() == page?.GetType())
        {
            _logger.LogDebug("目标页面与当前页面相同，跳过导航: {PageName}", page?.GetType().Name);
            return;
        }

        // 如果当前页面存在，将其压入导航栈
        if (CurrentPage != null)
        {
            _navigationStack.Push(CurrentPage);
            _logger.LogDebug("将 {CurrentPageName} 压入导航栈，栈深度: {StackCount}", CurrentPage.GetType().Name, _navigationStack.Count);
        }

        CurrentPage = page;
        _contentControl.Content = page;
        _logger.LogInformation("导航到页面: {PageName}", page?.GetType().Name);
    }

    public void GoBack()
    {
        if (!CanGoBack)
        {
            _logger.LogWarning("无法返回：导航栈为空");
            return;
        }

        if (_contentControl == null)
        {
            _logger.LogError("ContentControl为空，无法导航");
            return;
        }

        var previousPage = _navigationStack.Pop();
        _logger.LogDebug("从导航栈弹出页面: {PreviousPageName}，剩余栈深度: {StackCount}", previousPage.GetType().Name, _navigationStack.Count);

        CurrentPage = previousPage;
        _contentControl.Content = previousPage;
        _logger.LogInformation("返回到页面: {PageName}", previousPage.GetType().Name); ;
    }
}